﻿/*
*   学员端培训列表页面
*/
// 主页内容 由于angular 独立域（又懒加载）所以这里不能再用 jisuStuApp 了 只能angular.module('jisuStuHome') 获取模块后加controller 这样才能懒加载成功
angular.module('jisuStuHome').controller('stuTrainingList', ['$scope', '$rootScope', '$state', '$timeout', 'StuRequestTrain', 'RequestTrainService', 'sysUserInfo', 'Msg', 'MsgCon', 'LS', 'T', function ($scope, $rootScope, $state, $timeout, ST, RTS, sysUserInfo, Msg, MsgCon, LS, T) {

    // 打开指定的题库
    $scope.playOpenQue = function (taskId, knowObj) {
        //taskId  任务id
        //knowledgeId:知识架构id
        //passtype:合格条件   1 固定时长  2 通过率
        //pass   固定时长/通过率
        $state.go('home.stuquelist', { xid: taskId, knowledgeId: knowObj.knowledge_Id, passtype: knowObj.know_select_que_type, pass: knowObj.know_select_que_num, typeId: 0, knowledgeName: knowObj.knowledge_Name, parentId: knowObj.knowledge_Id, parentName: knowObj.knowledge_Name, arrangeId: taskId });
    }
    // 切换模式
    $scope.switchMode = function () {
        $scope.isIconList = !$scope.isIconList;
        LS.setLS("stuTrainListMode" + sysUserInfo.getuserinfo().user_ID, $scope.isIconList);
    }

    //切换类型
    $scope.selTypeList=function(index){
        $scope.stuListParams.pageIndex=1;
        $scope.stuListParams.searchType=index;
        $scope.getStuTrainList();
    }

    //文件预览 
    $scope.isFilePreview = { divhide: false, data: {} };
    // 文件预览
    $scope.filePreview = function (item) {
        $scope.isFilePreview.divhide = true;
        $scope.isFilePreview.data = item;
    }

    //初始化
    $scope.init = function () {

        // 图标模式 或者 列表模式
        $scope.isIconList = LS.getLS("stuTrainListMode" + sysUserInfo.getuserinfo().user_ID);
        if ($scope.isIconList == null || $scope.isIconList == undefined || $scope.isIconList == "") {   // 如果
            $scope.isIconList = true;
        }
        // 是否查看全部信息 4种
        $scope.isTrainList = "1";
        // 学员培训列表
        $scope.stuTrainList = [];
        //参数
        $scope.stuListParams = {
            orgID: sysUserInfo.getuserinfo().organization_ID,
            name: '',  // 按照培训任务名称搜索
            searchType:"",
            user_id: sysUserInfo.getuserinfo().user_ID,
            state: '1',
            org_Id: "",  // 用户组织机构
            role_Id: "", // 角色 
            user_groupId: "", //用户组
            orderType: "2", //1.名称 2.时间
            pageSize: 8,    // 10条 预留
            pageIndex: 1     // 页数
        }
        // 初始化担当前人的信息
        // 部门
        angular.forEach(sysUserInfo.getuserinfo().userOrgList, function (data, index, array) {
            $scope.stuListParams.org_Id += ($scope.stuListParams.org_Id.length <= 0 ? "" : ",") + data.organization_ID;
        });
        //角色 
        angular.forEach(sysUserInfo.getuserinfo().userRoleList, function (data, index, array) {
            $scope.stuListParams.role_Id += ($scope.stuListParams.role_Id.length <= 0 ? "" : ",") + data.roles_ID;
        });
        //用户组
        angular.forEach(sysUserInfo.getuserinfo().userGroupList, function (data, index, array) {
            $scope.stuListParams.user_groupId += ($scope.stuListParams.user_groupId.length <= 0 ? "" : ",") + data.userGroup_ID;
        })
        $scope.getStuTrainList();
    }
    $scope.reloadLoding = false;

    //刷新单击事件
    $scope.reloadProgress = function ($event, obj) {
        $event.stopPropagation();
        if ($scope.reloadLoding) {
            Msg.warning("提示", "上一个任务还未刷新完成，请稍后。。。");
            return;
        }
        //遍历任务里有的属性
        //type		类型 0.都没有 1.任务中有试卷  2.有题库 3.两个都有
        var a = "";
        var arrange = [];
        if (typeof (obj.arragetype) == "string") {
            arrange = JSON.parse(obj.arragetype);
        } else if (typeof (obj.arragetype) == "object") {
            arrange = obj.arragetype;
        }
        // var arrange = JSON.parse(obj.arragetype);
        var haspaper = false;
        var hastk = false;
        arrange.forEach(function (data, index) {
            //试卷
            if (data.hasOwnProperty("kseList") && data.kseList.length > 0) {
                haspaper = true;
            }
            //知识架构
            if (data.hasOwnProperty("kssList") && data.kssList.length > 0) {
                data.kscList.forEach(function (data1, index1) {
                    if (data1.hasOwnProperty("know_select_que") && data1.know_select_que) {
                        hastk = true;
                    }
                })
            }
        });
        //都有
        if (hastk && haspaper) {
            a = "3";
            //只有试卷
        } else if (haspaper && !hastk) {
            a = "1";
            //只有题库
        } else if (!haspaper && hastk) {
            a = "2";
            //都没有
        } else {
            a = "0";
        }
        $scope.getProgressCount(obj, a);
    }
    //请求刷新
    $scope.getProgressCount = function (obj, type) {
        $scope.reloadLoding = true;
        // Msg.warning("提示", "任务【" + obj.name + "】正在刷新进度。。。");
        ST.modifyTime({ arrangeid: obj.id, userid: sysUserInfo.getuserinfo().user_ID, type: type }).then(function (response) {
            $scope.reloadLoding = false;
            if (response.errorcode == 0) {
                Msg.success("刷新成功！");
                obj.completeCount = response.data;
            } else {
                Msg.error("刷新失败！");
            }
        });
    }
    $scope.commonUrl = "";
    

    // loading
    $scope.newloading = false;
    //回车
    $scope.findKeyEnter = function (e) {
        var keycode = window.event ? e.keyCode : e.which;
        if (keycode == 13) {
            $scope.getStuTrainList();
        }
    }
    //获取学员 任务列表
    $scope.getStuTrainList = function () {
        if ($scope.newloading)
            return; //禁止狂点
        $scope.newloading = true;
        //请求数据
        ST.findStudentStage($scope.stuListParams).then(function (response) {
            $scope.newloading = false;
            if (response.errorcode == "0") { // 成功                
                $scope.stuTrainList = response.datas;
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
                // 默认展开第一个
                if ($scope.stuTrainList.length > 0 && $scope.isTrainList != 5 && $scope.isTrainList != 6 && !$scope.isIconList) {
                    $scope.stageIsShow($scope.stuTrainList[0]);
                }
                $scope.stuTrainList.forEach(function (data, index) {
                    data.completeCount = parseFloat(data.completeCount);
                    data.courser_count = parseFloat(data.courser_count);
                });
                console.log($scope.stuTrainList);
            } else {
                Msg.error('数据请求失败！');
            }
        }, function errorCallback() {
            $scope.newloading = false;
            console.log("连接服务器失败！");
        });

        //        // 默认展开第一个
        //        if ($scope.stuTrainList.length > 0 && $scope.isTrainList != 5 && !$scope.isIconList) {
        //            $scope.stageIsShow($scope.stuTrainList[0]);
        //        }


    }

    $scope.init();

    // *****************************任务预览*********************/
    // 任务详细
    $scope.previewShow = false;
    $scope.previewObj = {};
    // 任务详细页面
    $scope.taskPreview = function (taskObj) {

        $state.go("home.train.publictask",{renwuobj:taskObj});
        // $scope.previewShow = true;
        // $scope.previewObj = taskObj;
    }

    $scope.closePreview = function () {
        $scope.previewShow = false;
    }
    //任务展开
    $scope.stageIsShow = function (stageTrainObj) {
        if ($scope.isTrainList == 5) {
            Msg.warning("提示", "任务已过期!");
            return;
        }
        if ($scope.isTrainList == 6) {
            Msg.warning("提示", "未到任务开始时间!");
            return;
        }
        $scope.taskPreview(stageTrainObj);
        
    }


    // 选择要筛选的
    $scope.selTrainList = function (type) {
        $scope.stuListParams.state = type; //查看全部的时候从数据库筛选
        if (type == 5 || type == 6) {
            $scope.isIconList = false;
        }
        $scope.isTrainList = type;
        $scope.stuListParams.name = '';
        $scope.getStuTrainList();
    }


    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 8, // 每页显示多少条        
        perPageOptions: [8, 16],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            if ((parseInt($scope.stuListParams.pageIndex) != parseInt($scope.paginationConf.currentPage)) || (parseInt($scope.stuListParams.pageSize) != parseInt($scope.paginationConf.itemsPerPage))) {
                if ($scope.paginationConf.currentPage == 0) { $scope.paginationConf.currentPage = 1; }
                $scope.stuListParams.pageIndex = $scope.paginationConf.currentPage;
                $scope.stuListParams.pageSize = $scope.paginationConf.itemsPerPage;
                $scope.getStuTrainList();
            }
        }
    };
    /******************************************************************直播***************************************************************************/
    $scope.liveParams = {
        orgID: sysUserInfo.getuserinfo().organization_ID,
        user_id: sysUserInfo.getuserinfo().user_ID,
        org_Id: $scope.stuListParams.org_Id,  // 用户组织机构
        role_Id: $scope.stuListParams.role_Id, // 角色 
        user_groupId: $scope.stuListParams.user_groupId, //用户组
        xtype: "6", //直播
        pageSize: 10,    // 10条 预留
        pageIndex: 1     // 页数
    }
    $scope.sectionList = [];
    //查询直播列表
    $scope.findLiveList = function () {
        $scope.newloading = true;
        $scope.isTrainList = "7";
        ST.findSectionList($scope.liveParams).then(function (response) {
            $scope.newloading = false;
            if (response.errorcode == 0 && response.datas != undefined) {
                $scope.sectionList = response.datas;
                $scope.sectionList.forEach(function (data, index) {
                    if (data.chapterJson != null && data.chapterJson != undefined && data.chapterJson.length > 0) {
                        data.chapterJson = JSON.parse(data.chapterJson);
                    } else {
                        data.chapterJson = {};
                    }
                });
                $scope.paginationConfLive.totalItems = response.numCount;
                $scope.paginationConfLive.currentPage = response.pageIndex;
            } else if (response.errorcode != 0) {
                Msg.warning("获取直播列表失败！");
            }
        });
    }
    //直播
    $scope.playLive = function (section) {

        // window.location = section.chapterJson.liveObj.stuHtm + "?lid=" + section.chapterJson.liveObj.roomId;
        window.location ="/live/index.html?id="+section.chapterJson.liveId;
    }
    //配置分页
    $scope.paginationConfLive = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConfLive.currentPage = $scope.paginationConfLive.currentPage == 0 ? 1 : $scope.paginationConfLive.currentPage;
            if ((parseInt($scope.liveParams.pageIndex) != parseInt($scope.paginationConfLive.currentPage)) || (parseInt($scope.liveParams.pageSize) != parseInt($scope.paginationConfLive.itemsPerPage))) {
                if ($scope.paginationConfLive.currentPage == 0) { $scope.paginationConfLive.currentPage = 1; }
                $scope.liveParams.pageIndex = $scope.liveParams.currentPage;
                $scope.liveParams.pageSize = $scope.liveParams.itemsPerPage;
                $scope.findLiveList();
            }
        }
    };

} ]);

